import React from 'react';
import {injectIntl, language} from './intl';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import TextInput from 'Shared/components/TextInput';
import {dealerConditions, PERMISSION} from './constants';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import TagSelect from 'Shared/components/TagSelect';
import WrappedSelect from '../common/WrappedSelect';
import {repairType, secondClaimStatus} from '../Enum';
import routes from './routes';
import {hasPermissions} from './utils';
const FormItem = Form.Item;
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
/*eslint-disable  react/prefer-stateless-function*/
class QueryPanel extends React.PureComponent {
    render() {
        const dealerInfo = this.props.conditions.dealerId ? {
            id: this.props.conditions.dealerId,
            code: this.props.conditions.dealerCode,
            name: this.props.conditions.dealerName
        } : null;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.updateMore))
            operations.push(<Button key="updateMore" href={`#${routes.updateMore.url()}`}>{this.props.intl.formatMessage({
                id: 'queryPanel.operation.updateMore',
                defaultMessage: '批量编辑'
            })}</Button>);
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.code',
                                    defaultMessage: '索赔单号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={this.props.conditions.code}
                                    onPressEnter={this.props.onConditionsPressEnter}
                                    onBlur={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>
                        {this.props.brands.length > 1 ? <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.brandId',
                                    defaultMessage: '品牌'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    name="brandId"
                                    allowClear
                                    value={this.props.conditions.brandId}
                                    options={this.props.brands}
                                    onChange={this.props.onConditionsChange} />
                            </FormItem>
                        </Col> : null}
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerId',
                                    defaultMessage: '总代'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DealerSelectPanel
                                    value={dealerInfo}
                                    conditions={dealerConditions}
                                    onSelect={this.props.onSelect} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.causePartCode',
                                    defaultMessage: '主损件编号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="causePartCode"
                                    value={this.props.conditions.causePartCode}
                                    onPressEnter={this.props.onConditionsPressEnter}
                                    onBlur={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.causePartName',
                                    defaultMessage: '主损件名称'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="causePartName"
                                    value={this.props.conditions.causePartName}
                                    onPressEnter={this.props.onConditionsPressEnter}
                                    onBlur={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.vin',
                                    defaultMessage: 'VIN'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="vin"
                                    value={this.props.conditions.vin}
                                    onPressEnter={this.props.onConditionsPressEnter}
                                    onBlur={this.props.onConditionsChange} />
                            </FormItem>
                        </Col><Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.status',
                                    defaultMessage: '状态'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    name="status"
                                    allowClear
                                    value={this.props.conditions.status}
                                    options={secondClaimStatus.toList(language)}
                                    onChange={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.repairType',
                                    defaultMessage: '索赔类型'
                                })}
                                {...FORM_ROW_OPTIONS.item}>
                                <TagSelect
                                    name="repairType"
                                    value={this.props.conditions.repairType}
                                    options={repairType.toList(language)}
                                    onChange={this.props.onConditionsChange} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="operation-buttons">
                        <Col span={6}>
                            <Button
                                key="query"
                                type="primary"
                                onClick={this.props.querySearch}
                                loading={this.props.loading}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}
                            </Button>
                            <Button key="clear" onClick={this.props.onClickReset}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                        <Col span={18} className="col-align-right">
                            {operations}
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    brands: PropTypes.array.isRequired,
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onConditionsPressEnter: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    intl: PropTypes.object
};

const EMPTY_ARRAY = [];
import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, selectDealer} from './actions';
import {createSelector} from 'reselect';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getBrands = createSelector(
    state => state.getIn(['page', 'domainData', 'brands', 'data']),
    brands => {
        const data = brands.toJS() || EMPTY_ARRAY;
        return data.map(item => ({
            value: item.id,
            text: item.name
        }));
    }
);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
    brands: getBrands(state)
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (value, name) => dispatch(saveQueryCondition(name, value)),
    onConditionsPressEnter: (value, name) => {
        dispatch(saveQueryCondition(name, value));
        dispatch(querySearch());
    },
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
    onSelect: data => dispatch(selectDealer(data)),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(QueryPanel));
